//
//  PropertyReadingWritingClass.m
//  Portable Traffic Signal Assistor
//
//  Created by Hao Xu on 5/31/12.
//  Copyright (c) 2012 TTU. All rights reserved.
//


#import "PropertyReadingWritingClass.h"

@implementation PropertyReadingWritingClass

+ (void) writeProjectIDToPropertyFile:(NSString *)projectID
{

NSString *error;
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSString *plistPath;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                          NSUserDomainMask, YES) objectAtIndex:0];
plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
//    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
//        plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
//    }
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
if (plistXML==nil) {
    NSMutableDictionary *plistDict = [NSDictionary dictionaryWithObjects:
                                      [NSArray arrayWithObjects: projectID, nil]
                                                                 forKeys:[NSArray arrayWithObjects: @"projectID", nil]];
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
                                                                   format:NSPropertyListXMLFormat_v1_0
                                                         errorDescription:&error];
    if(plistData) {
        [plistData writeToFile:plistPath atomically:YES];
    }
    else {
        NSLog(error);
        [error release];
    }
    
}else
{
    NSMutableDictionary *plistDict = (NSMutableDictionary *)[NSPropertyListSerialization
                                                             propertyListFromData:plistXML
                                                             mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                                             format:&format
                                                             errorDescription:&errorDesc];
    [plistDict setObject:projectID forKey:@"projectID"];
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
                                                                   format:NSPropertyListXMLFormat_v1_0
                                                         errorDescription:&error];
    if(plistData) {
        [plistData writeToFile:plistPath atomically:YES];
    }
    else {
        NSLog(error);
        [error release];
    }
    
    
}
}
+ (NSString*) readProjectIDFromPropertyFile
{
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSString *plistPath;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                              NSUserDomainMask, YES) objectAtIndex:0];
    plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
    //    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
    //        plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    //    }
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    NSMutableDictionary *temp = (NSMutableDictionary *)[NSPropertyListSerialization
                                                        propertyListFromData:plistXML
                                                        mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                                        format:&format
                                                        errorDescription:&errorDesc];
    if (!temp) {
        NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
        return nil;
    }
    NSString *projectID = [temp objectForKey:@"projectID"];
    
    return projectID;
}

+ (void) writeCenterLatitudeToPropertyFile:(NSString *)centerLatitude
{
    
    NSString *error;
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSString *plistPath;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                              NSUserDomainMask, YES) objectAtIndex:0];
    plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
    //    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
    //        plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    //    }
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    if (plistXML==nil) {
        NSMutableDictionary *plistDict = [NSDictionary dictionaryWithObjects:
                                          [NSArray arrayWithObjects: centerLatitude, nil]
                                                                     forKeys:[NSArray arrayWithObjects: @"centerLatitude", nil]];
        NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
                                                                       format:NSPropertyListXMLFormat_v1_0
                                                             errorDescription:&error];
        if(plistData) {
            [plistData writeToFile:plistPath atomically:YES];
        }
        else {
            NSLog(error);
            [error release];
        }
        
    }else
    {
        NSMutableDictionary *plistDict = (NSMutableDictionary *)[NSPropertyListSerialization
                                                                 propertyListFromData:plistXML
                                                                 mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                                                 format:&format
                                                                 errorDescription:&errorDesc];
        [plistDict setObject:centerLatitude forKey:@"centerLatitude"];
        NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
                                                                       format:NSPropertyListXMLFormat_v1_0
                                                             errorDescription:&error];
        if(plistData) {
            [plistData writeToFile:plistPath atomically:YES];
        }
        else {
            NSLog(error);
            [error release];
        }
        
        
    }
}
+ (NSString*) readCenterLatitudeFromPropertyFile
{
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSString *plistPath;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                              NSUserDomainMask, YES) objectAtIndex:0];
    plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
    //    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
    //        plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    //    }
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    NSMutableDictionary *temp = (NSMutableDictionary *)[NSPropertyListSerialization
                                                        propertyListFromData:plistXML
                                                        mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                                        format:&format
                                                        errorDescription:&errorDesc];
    if (!temp) {
        NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
        return @"0";
    }
    NSString *centerLatitude = [temp objectForKey:@"centerLatitude"];
    
    return centerLatitude;
}

+ (void) writeCenterLongitudeToPropertyFile:(NSString *)centerLongitude
{
    
    NSString *error;
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSString *plistPath;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                              NSUserDomainMask, YES) objectAtIndex:0];
    plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
    //    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
    //        plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    //    }
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    if (plistXML==nil) {
        NSMutableDictionary *plistDict = [NSDictionary dictionaryWithObjects:
                                          [NSArray arrayWithObjects: centerLongitude, nil]
                                                                     forKeys:[NSArray arrayWithObjects: @"centerLongitude", nil]];
        NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
                                                                       format:NSPropertyListXMLFormat_v1_0
                                                             errorDescription:&error];
        if(plistData) {
            [plistData writeToFile:plistPath atomically:YES];
        }
        else {
            NSLog(error);
            [error release];
        }
        
    }else
    {
        NSMutableDictionary *plistDict = (NSMutableDictionary *)[NSPropertyListSerialization
                                                                 propertyListFromData:plistXML
                                                                 mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                                                 format:&format
                                                                 errorDescription:&errorDesc];
        [plistDict setObject:centerLongitude forKey:@"centerLongitude"];
        NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
                                                                       format:NSPropertyListXMLFormat_v1_0
                                                             errorDescription:&error];
        if(plistData) {
            [plistData writeToFile:plistPath atomically:YES];
        }
        else {
            NSLog(error);
            [error release];
        }
        
        
    }
}
+ (NSString*) readCenterLongitudeFromPropertyFile
{
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSString *plistPath;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                              NSUserDomainMask, YES) objectAtIndex:0];
    plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
    //    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
    //        plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    //    }
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    NSMutableDictionary *temp = (NSMutableDictionary *)[NSPropertyListSerialization
                                                        propertyListFromData:plistXML
                                                        mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                                        format:&format
                                                        errorDescription:&errorDesc];
    if (!temp) {
        NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
        return @"0";
    }
    NSString *centerLongitude = [temp objectForKey:@"centerLongitude"];
    
    return centerLongitude;
}

+ (void) writeLatitudeDeltaToPropertyFile:(NSString *)latitudeDelta
{
    
    NSString *error;
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSString *plistPath;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                              NSUserDomainMask, YES) objectAtIndex:0];
    plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
    //    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
    //        plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    //    }
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    if (plistXML==nil) {
        NSMutableDictionary *plistDict = [NSDictionary dictionaryWithObjects:
                                          [NSArray arrayWithObjects: latitudeDelta, nil]
                                                                     forKeys:[NSArray arrayWithObjects: @"latitudeDelta", nil]];
        NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
                                                                       format:NSPropertyListXMLFormat_v1_0
                                                             errorDescription:&error];
        if(plistData) {
            [plistData writeToFile:plistPath atomically:YES];
        }
        else {
            NSLog(error);
            [error release];
        }
        
    }else
    {
        NSMutableDictionary *plistDict = (NSMutableDictionary *)[NSPropertyListSerialization
                                                                 propertyListFromData:plistXML
                                                                 mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                                                 format:&format
                                                                 errorDescription:&errorDesc];
        [plistDict setObject:latitudeDelta forKey:@"latitudeDelta"];
        NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
                                                                       format:NSPropertyListXMLFormat_v1_0
                                                             errorDescription:&error];
        if(plistData) {
            [plistData writeToFile:plistPath atomically:YES];
        }
        else {
            NSLog(error);
            [error release];
        }
        
        
    }
}
+ (NSString*) readLatitudeDeltaFromPropertyFile
{
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSString *plistPath;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                              NSUserDomainMask, YES) objectAtIndex:0];
    plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
    //    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
    //        plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    //    }
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    NSMutableDictionary *temp = (NSMutableDictionary *)[NSPropertyListSerialization
                                                        propertyListFromData:plistXML
                                                        mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                                        format:&format
                                                        errorDescription:&errorDesc];
    if (!temp) {
        NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
        return nil;
    }
    NSString *latitudeDelta = [temp objectForKey:@"latitudeDelta"];
    
    return latitudeDelta;
}

+ (void) writeLongitudeDeltaToPropertyFile:(NSString *)longitudeDelta
{
    
    NSString *error;
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSString *plistPath;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                              NSUserDomainMask, YES) objectAtIndex:0];
    plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
    //    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
    //        plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    //    }
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    if (plistXML==nil) {
        NSMutableDictionary *plistDict = [NSDictionary dictionaryWithObjects:
                                          [NSArray arrayWithObjects: longitudeDelta, nil]
                                                                     forKeys:[NSArray arrayWithObjects: @"longitudeDelta", nil]];
        NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
                                                                       format:NSPropertyListXMLFormat_v1_0
                                                             errorDescription:&error];
        if(plistData) {
            [plistData writeToFile:plistPath atomically:YES];
        }
        else {
            NSLog(error);
            [error release];
        }
        
    }else
    {
        NSMutableDictionary *plistDict = (NSMutableDictionary *)[NSPropertyListSerialization
                                                                 propertyListFromData:plistXML
                                                                 mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                                                 format:&format
                                                                 errorDescription:&errorDesc];
        [plistDict setObject:longitudeDelta forKey:@"longitudeDelta"];
        NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
                                                                       format:NSPropertyListXMLFormat_v1_0
                                                             errorDescription:&error];
        if(plistData) {
            [plistData writeToFile:plistPath atomically:YES];
        }
        else {
            NSLog(error);
            [error release];
        }
        
        
    }
}
+ (NSString*) readLongitudeDeltaFromPropertyFile
{
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSString *plistPath;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                              NSUserDomainMask, YES) objectAtIndex:0];
    plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
    //    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
    //        plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    //    }
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    NSMutableDictionary *temp = (NSMutableDictionary *)[NSPropertyListSerialization
                                                        propertyListFromData:plistXML
                                                        mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                                        format:&format
                                                        errorDescription:&errorDesc];
    if (!temp) {
        NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
        return nil;
    }
    NSString *longitudeDelta = [temp objectForKey:@"longitudeDelta"];
    
    return longitudeDelta;
}

+ (void) writeMapRegionToPropertyFile:(MKCoordinateRegion )mapRegion
{
    
    [self writeCenterLatitudeToPropertyFile:[NSString stringWithFormat:@"%f",mapRegion.center.latitude]];
    [self writeCenterLongitudeToPropertyFile:[NSString stringWithFormat:@"%f",mapRegion.center.longitude]];
    [self writeLatitudeDeltaToPropertyFile:[NSString stringWithFormat:@"%f",mapRegion.span.latitudeDelta]];
    [self writeLongitudeDeltaToPropertyFile:[NSString stringWithFormat:@"%f",mapRegion.span.longitudeDelta]];
}
+ (MKCoordinateRegion) readMapRegionFromPropertyFile
{
//    CLLocationCoordinate2D c={0,0};
//    MKCoordinateRegion mapRegion=MKCoordinateRegionMake(centerCoordinate, span);
    CLLocationCoordinate2D centerCoordinate={[[self readCenterLatitudeFromPropertyFile] floatValue],[[self readCenterLongitudeFromPropertyFile] floatValue]};
    MKCoordinateSpan span=MKCoordinateSpanMake([[self readLatitudeDeltaFromPropertyFile] floatValue], [[self readLongitudeDeltaFromPropertyFile] floatValue]);
    MKCoordinateRegion mapRegion=MKCoordinateRegionMake(centerCoordinate, span);
    return mapRegion;
}
+ (void) writeXMLFileName:(NSString *)fileName
{
    NSString *error;
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSString *plistPath;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                              NSUserDomainMask, YES) objectAtIndex:0];
    plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
    //    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
    //        plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    //    }
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    if (plistXML==nil) {
        NSMutableDictionary *plistDict = [NSDictionary dictionaryWithObjects:
                                          [NSArray arrayWithObjects: fileName, nil]
                                                                     forKeys:[NSArray arrayWithObjects: @"xmlFileName", nil]];
        NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
                                                                       format:NSPropertyListXMLFormat_v1_0
                                                             errorDescription:&error];
        if(plistData) {
            [plistData writeToFile:plistPath atomically:YES];
        }
        else {
            NSLog(error);
            [error release];
        }
        
    }else
    {
        NSMutableDictionary *plistDict = (NSMutableDictionary *)[NSPropertyListSerialization
                                                                 propertyListFromData:plistXML
                                                                 mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                                                 format:&format
                                                                 errorDescription:&errorDesc];
        [plistDict setObject:fileName forKey:@"xmlFileName"];
        NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
                                                                       format:NSPropertyListXMLFormat_v1_0
                                                             errorDescription:&error];
        if(plistData) {
            [plistData writeToFile:plistPath atomically:YES];
        }
        else {
            NSLog(error);
            [error release];
        }
        
        
    }
}
+ (NSString*) readXMLFileName
{
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    NSString *plistPath;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                              NSUserDomainMask, YES) objectAtIndex:0];
    plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
    //    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
    //        plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    //    }
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    NSMutableDictionary *temp = (NSMutableDictionary *)[NSPropertyListSerialization
                                                        propertyListFromData:plistXML
                                                        mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                                        format:&format
                                                        errorDescription:&errorDesc];
    if (!temp) {
        NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
        return nil;
    }
    NSString *fileName = [temp objectForKey:@"xmlFileName"];
    
    return fileName;
}

@end
